<?php

/* ========================================================================= */
/* This file is part of the HypnoPHP Framework Library                       */
/* Copyright (c) 2009-2010, Radu Buzea                                       */
/* ========================================================================= */

/**
* @author Radu Buzea
* @copyright Copyright (c) 2009-2010, Radu Buzea (www.hypnosis.ro)
* @license GNU Lesser General Public License (www.gnu.org/copyleft/lesser.html)
* @package HypnoPHP
* @category Core
* @version 1.0.3
*/

// ============================================================================
// Final class: HErrorPage
// ============================================================================

final class HErrorPage extends HBaseController
{

	/**
	* Search and loads an error page file
	*
	* @param integer $error_no The error code
	* @return string (the filename path) or false on error
	* @access public
	* @static
	*/
	static public function getErrorPageFile($error_no = 0)
	{
		global $_HAPPLICATION_PATHS;
		$error_files = array(
			$_HAPPLICATION_PATHS['app_errors'] . DS . $error_no . '.php',
			$_HAPPLICATION_PATHS['app_errors'] . DS . 'error' . $error_no . '.php',
			$_HAPPLICATION_PATHS['system_errors'] . DS . $error_no . '.php',
			$_HAPPLICATION_PATHS['system_errors'] . DS . 'error' . $error_no . '.php',
		);
		foreach ($error_files as $file) {
			if (is_file($file)) { return $file; }
		}
		return false;
	}

	/**
	* Return an error page
	*
	* @return string
	* @access public
	*/
	public function errorPage()
	{
		$result = '';
		$this->disable_layout = true;
		$func_args = func_get_args();
		if (isset($func_args[0]) && intval($func_args[0]) > 0) {
			$error_no = intval($func_args[0]);
			$error_page_file = HErrorPage::getErrorPageFile($error_no);
			switch ($error_no) {
				case 400:
					HResponse::set_header('HTTP/1.0 400 Bad Request');
					break;
				case 401:
					HResponse::set_header('HTTP/1.0 401 Unauthorized');
					break;
				case 403:
					HResponse::set_header('HTTP/1.0 403 Forbidden');
					break;
				case 404:
					HResponse::set_header('HTTP/1.0 404 Not Found');
					break;
				case 405:
					HResponse::set_header('HTTP/1.0 405 Method Not Allowed');
					break;
			}
		}
		if (isset($error_page_file) && $error_page_file) {
			ob_start();
			include $error_page_file;
			$result = ob_get_clean();
		}
		return $result;
	}

}
